<?php

namespace App\Admin\Actions\Good;

use App\Model\Note;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Remark extends RowAction
{
    public $name = '备注';

    public function form()
    {
        // 文本输入框
        $this->text('remark-' . $this->getRow()->id, '备注')
            ->default($this->getRow()->lastAdminNote->content ?? '')
            ->rules('required');
    }

    public function handle(Model $model, Request $request)
    {
        $remarkModel = new Note();
        $remarkModel->fill([
            'goods_id' => $this->getRow()->id,
            'content' => $request->get('remark-' . $this->getRow()->id),
            'user_user_id' => Admin::user()->id
        ]);

        if ($remarkModel->save() !== false) {
            return $this->response()->success('操作成功')->refresh();
        }
        return $this->response()->error('操作失败')->refresh();
    }

}
